/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.zx.sys.msg.push.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jms.Queue;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.app.util.wx.NioWebSocketHandler;
import com.jeesite.modules.app.util.wx.SocketMessage;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.zx.sys.msg.push.entity.ZxMsgPush;
import com.jeesite.modules.zx.sys.msg.push.service.ZxMsgPushService;
import com.jeesite.modules.zx.user.entity.ZxUser;
import com.jeesite.modules.zx.user.service.ZxUserService;

/**
 * 系统消息表Controller
 * @author zpp
 * @version 2020-01-17
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/sys/msg/push/zxMsgPush")
public class ZxMsgPushController extends BaseController {

	@Autowired
	private ZxMsgPushService zxMsgPushService;
	@Autowired
	private ZxUserService zxUserService;
	@Autowired
	private EmpUserService empUserService;
	@Autowired
    private Queue queue;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZxMsgPush get(String id, boolean isNewRecord) {
		return zxMsgPushService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:view")
	@RequestMapping(value = {"list", ""})
	public String list(ZxMsgPush zxMsgPush, Model model) {
		model.addAttribute("zxMsgPush", zxMsgPush);
		return "modules/zx/sys/msg/push/zxMsgPushList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZxMsgPush> listData(ZxMsgPush zxMsgPush, HttpServletRequest request, HttpServletResponse response) {
		zxMsgPush.setPage(new Page<>(request, response));
		Page<ZxMsgPush> page = zxMsgPushService.findPage(zxMsgPush);
		for(ZxMsgPush push:page.getList()){
			String receiveCode = push.getReceiveCode();
			
			EmpUser empuser=new EmpUser();
			empuser.setUserCode(receiveCode);
			List<EmpUser> findList = empUserService.findList(empuser);
			if(findList!=null&&findList.size()>0){
				push.setReceiveUserName(findList.get(0).getUserName());
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:view")
	@RequestMapping(value = "form")
	public String form(ZxMsgPush zxMsgPush, Model model) {
		ZxUser arg0=new ZxUser();
		List<String> list=new ArrayList<String>();
		list.add("none");
		arg0.getSqlMap().getWhere().and("user_type", QueryType.NOT_IN, list);
		List<ZxUser> findList = zxUserService.findList(arg0);
		model.addAttribute("zxMsgPush", zxMsgPush);
		model.addAttribute("userList",findList);
		return "modules/zx/sys/msg/push/zxMsgPushForm";
	}

	/**
	 * 保存消息推送表
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZxMsgPush zxMsgPush) {
		zxMsgPush.setSendDate(new Date());
		zxMsgPush.setReadStatus("0");
		zxMsgPushService.save(zxMsgPush);
		String sendUserCode = zxMsgPush.getReceiveCode();
		JSONObject.parseObject(JsonMapper.toJson(zxMsgPush));
		 try {
			ZxMsgPush entity=new ZxMsgPush();
			entity.setReceiveCode(sendUserCode);
			entity.setReadStatus("0");
			long findCount = zxMsgPushService.findCount(entity);
			SocketMessage sm=new SocketMessage();
			sm.setType("systeminfo");
			sm.setNum(findCount);
			sm.setUserId(sendUserCode);
			jmsMessagingTemplate.convertAndSend(queue,JsonMapper.toJson(sm));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return renderResult(Global.TRUE, text("保存消息推送表成功！"));
	}
	
	/**
	 * 停用消息推送表
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(ZxMsgPush zxMsgPush) {
		zxMsgPush.setStatus(ZxMsgPush.STATUS_DISABLE);
		zxMsgPushService.updateStatus(zxMsgPush);
		return renderResult(Global.TRUE, text("停用消息推送表成功"));
	}
	
	/**
	 * 启用消息推送表
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(ZxMsgPush zxMsgPush) {
		zxMsgPush.setStatus(ZxMsgPush.STATUS_NORMAL);
		zxMsgPushService.updateStatus(zxMsgPush);
		return renderResult(Global.TRUE, text("启用消息推送表成功"));
	}
	
	/**
	 * 删除消息推送表
	 */
	@RequiresPermissions("zx:sys:msg:push:zxMsgPush:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZxMsgPush zxMsgPush) {
		zxMsgPushService.delete(zxMsgPush);
		return renderResult(Global.TRUE, text("删除消息推送表成功！"));
	}
	
}